
var sjs = {}

sjs.marker = function(ent) {
    return ent
}

sjs.ref = function(type) {
    return type
}

sjs.baseEntities = {
    inc: function(arg) {
        return arg
    },

    less: function(arg1, arg2) {
        return [arg1, arg2]
    },

    greater: function(arg1, arg2) {
        return [arg1, arg2]
    }
}

sjs.defines = {}

sjs.external = {
    registerFunctions: function(pairs) {
        return pairs
    },

    registerTypes: function(pairs) {
        return pairs
    }
}

sjs.collectionOf = function() {}

sjs.types = {
    Size: null,
    String: null
}

function moduleDef(env, defines) {
    env.registerTypes({
        Sequence: null,
        Element: null
    })

    env.registerFunctions({
        // length is a function
        // function length(sequence) { ... return 5 }
        length: {
            sequence: env.types.Sequence,
            returns: env.types.Size
        },

        // function swap(element1, element2) { ... }
        swap: {
            element1: env.ref(env.types.Element),
            element2: env.ref(env.types.Element)
        }
    })


    defines.sort = function (sequence) {
        var swapped = false
        do {
            for (var i = 0; env.less(i, env.length(sequence)); env.inc(i)) {
                if (env.greater(sequence[i], sequence[i+1])) {
                    env.swap(sequence[i], sequence[i+1])
                    swapped = true
                }
            }
        } while (swapped)
    }
}





